package com.cliff.joinus.activity.fragment;

import java.util.ArrayList;

import org.cybergarage.upnp.Device;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.cliff.joinus.JoinusControlPoint;
import com.cliff.joinus.JoinusMainBaseFragment;
import com.cliff.joinus.R;

public class RendererFragment extends JoinusMainBaseFragment {

	protected BroadcastReceiver mReceiver = null;
	protected ArrayAdapter<String> mAdapter = null;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		JoinusControlPoint cp = JoinusControlPoint.getInstance();
		ArrayList<String> udnList = cp.getRendererUdnList();
		udnList.add(0, null);		
		String udnSelected = cp.getSelectedRendererUdn();
		if (!isArrayListContainsString(udnList, udnSelected)) {
			udnSelected = null;
			cp.setSelectedRendererUdn(udnSelected);
		}

		mAdapter = new ArrayAdapter<String>(getActivity(),
				R.layout.item_line_name_desc, udnList) {
			@Override
			public View getView(int position, View convertView, ViewGroup parent) {
				String udn = getItem(position);
				String udnSelected = JoinusControlPoint.getInstance().getSelectedRendererUdn();
				boolean isSelected = isEqual(udnSelected, udn);
				if (null == udn) {
					return getItemView(convertView, R.drawable.renderer_default,
							"Local Renderer", "127.0.0.1", isSelected);
				} else {
					Device dev = JoinusControlPoint.getInstance()
							.getDevice(udn);
					return getItemView(convertView, R.drawable.renderer_default,
							dev.getFriendlyName(), dev.getSSDPPacket()
									.getRemoteAddress(), isSelected);
				}
			}
		};

		setListAdapter(mAdapter);
		registerBroadcastReceiver();
		return super.onCreateView(inflater, container, savedInstanceState);
	}

	@Override
	public void onDestroyView() {
		// TODO Auto-generated method stub
		unregisterBroadcastReceiver();
		mAdapter = null;
		super.onDestroyView();
	}

	@Override
	public void onListItemClick(ListView l, View v, int position, long id) {
		// TODO Auto-generated method stub
		JoinusControlPoint cp = JoinusControlPoint.getInstance();
		String udnSelected = cp.getSelectedRendererUdn();
		String udn = (String) getListAdapter().getItem(position);
		if (isEqual(udn, udnSelected))
			return;
		
		cp.setSelectedRendererUdn(udn);
		refresh();
		super.onListItemClick(l, v, position, id);
	}

	@Override
	public void refresh() {
		mAdapter.notifyDataSetChanged();
	}

	protected boolean isEqual(String str1, String str2) {
		if (null == str1) {
			if (null == str2)
				return true;
			return false;
		}
		return str1.equals(str2);
	}

	protected boolean isArrayListContainsString(ArrayList<String> list,
			String value) {
		for (String p : list) {
			if (isEqual(p, value)) {
				return true;
			}
		}
		return false;
	}
	
	protected void registerBroadcastReceiver() {
		IntentFilter intentFilter = new IntentFilter(JoinusControlPoint.ACTION_DEVICE_CHANGE);
		mReceiver = new BroadcastReceiver() {

			@Override
			public void onReceive(Context context, Intent intent) {
				// TODO Auto-generated method stub
				if(intent.getBooleanExtra(JoinusControlPoint.EXTRA_IS_RENDERER, false)) {
					if (null == mAdapter)
						return;

					String udn = intent.getStringExtra(JoinusControlPoint.EXTRA_UDN);
					boolean isAdd = intent.getBooleanExtra(JoinusControlPoint.EXTRA_IS_ADD, false);
					if (isAdd)
						mAdapter.add(udn);
					else {
						mAdapter.remove(udn);
					}
					mAdapter.notifyDataSetChanged();
				}
			}
			
		};
		getActivity().registerReceiver(mReceiver , intentFilter);
	}
	
	protected void unregisterBroadcastReceiver() {
		getActivity().unregisterReceiver(mReceiver);
		mReceiver = null;
	}
}
